package com.waterworld.watch.location.activity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.BounceInterpolator;
import android.view.animation.Interpolator;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.Projection;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.Circle;
import com.amap.api.maps.model.CircleOptions;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.waterworld.watch.R;
import com.waterworld.watch.common.activity.BaseActivity;
import com.waterworld.watch.common.bean.ElectronicFenceBean;
import com.waterworld.watch.common.bean.LocationBean;
import com.waterworld.watch.common.customview.CommonPopupWindow;
import com.waterworld.watch.common.customview.CustomDialog;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.net.BaseResultBean;
import com.waterworld.watch.common.util.ScreenAdapterUtil;
import com.waterworld.watch.common.util.TimeUtils;
import com.waterworld.watch.common.util.ToastUtils;
import com.waterworld.watch.location.event.ElectronicFenceEvent;
import com.waterworld.watch.location.interfaces.ILocationManager;
import com.waterworld.watch.location.manager.LocationManager;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.carbswang.android.numberpickerview.library.NumberPickerView;

public class AddElectronicFenceActivity extends BaseActivity {

    @BindView(R.id.ll_parent)
    LinearLayout ll_parent;

    @BindView(R.id.header_parent)
    LinearLayout header_parent;

    @BindView(R.id.header_back)
    ImageView header_back;

    @BindView(R.id.header_title)
    TextView header_title;

    @BindView(R.id.header_confirm)
    TextView header_confirm;

    @BindView(R.id.et_fence_name)
    EditText et_fence_name;

    @BindView(R.id.rl_remind_way)
    RelativeLayout rl_remind_way;

    @BindView(R.id.iv_remind_way)
    ImageView iv_remind_way;

    @BindView(R.id.rg_remind_way)
    RadioGroup rg_remind_way;

    @BindView(R.id.rb_remind_enter)
    RadioButton rb_remind_enter;

    @BindView(R.id.rb_remind_leave)
    RadioButton rb_remind_leave;

    @BindView(R.id.rb_remind_not_enter_on_time)
    RadioButton rb_remind_not_enter_on_time;

    @BindView(R.id.sb_fence_range)
    SeekBar sb_fence_range;

    @BindView(R.id.tv_fence_range)
    TextView tv_fence_range;

    @BindView(R.id.rl_fence_range)
    RelativeLayout rl_fence_range;

    @BindView(R.id.map)
    MapView mMapView;

    private AMap aMap = null;
    private static final int RANGE_MIN = 300;
    private static final int RANGE = 2000;
    private ElectronicFenceBean bean;
    private ILocationManager iLocationManager = LocationManager.getInstance();

    private CommonPopupWindow popupWindow;
    private CommonPopupWindow.LayoutGravity layoutGravity;

    private CustomDialog mDialog;
    Object circle;
    Object marker;
    private static boolean isExtend = true;
    private boolean[] weekArray = {false,false,false,false,false,false,false};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_electronic_fence);
        ButterKnife.bind(this);
        mMapView.onCreate(savedInstanceState);
        if(aMap == null){
            aMap = mMapView.getMap();
        }
        initView();
        onClick();

    }

    @SuppressLint("SetTextI18n")
    private void initView(){
        setViewSize(header_parent, RelativeLayout.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this)/10);
        header_back.setVisibility(View.VISIBLE);
        header_title.setVisibility(View.VISIBLE);
        header_confirm.setVisibility(View.VISIBLE);

        if (getIntent().getSerializableExtra("fence") != null) {
            header_title.setText(getString(R.string.edit_fence));
            bean = (ElectronicFenceBean) getIntent().getSerializableExtra("fence");
            et_fence_name.setText(bean.getName());
            switch (bean.getRemindWay()) {
                case 1:
                    rb_remind_enter.setChecked(true);
                    break;
                case 2:
                    rb_remind_leave.setChecked(true);
                    break;
                case 3:
                    rb_remind_not_enter_on_time.setChecked(true);
                    break;
                default:
                    break;
            }
            LatLng latLng = new LatLng(Double.parseDouble(bean.getFenceLat()),Double.parseDouble(bean.getFenceLng()));
            CameraUpdate mCameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng,15,0,0));
            aMap.moveCamera(mCameraUpdate);

            //设置和SeekBar一起拖动
            ViewTreeObserver vto = sb_fence_range.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    sb_fence_range.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    sb_fence_range.setProgress(bean.getFenceRange() - RANGE_MIN);
                }
            });
            tv_fence_range.setText(bean.getFenceRange()+getString(R.string.meter));

            marker = addMarkerToMap(latLng, BitmapFactory.decodeResource(getResources(), R.drawable.location_marker), 0.5f, 0.9f, false, 0);
            circle = addCircleToMap(latLng,bean.getFenceRange(), getResources().getColor(R.color.blue_4));

            header_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iLocationManager != null) {
                        if (bean.getRemindWay() == 3 && (bean.getRemindTime() == null || bean.getRemindDays() == null || bean.getRemindTime().equals("") || bean.getRemindDays().equals(""))) {
                            mDialog = new CustomDialog(AddElectronicFenceActivity.this, getString(R.string.warm_prompt), getString(R.string.fence_notice), getString(R.string.confirm), new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    mDialog.dismiss();
                                }
                            });
                            mDialog.setCanotBackPress();
                            mDialog.setCanceledOnTouchOutside(false);
                            mDialog.show();
                        } else {
                            if (bean.getRemindWay() != 3) {
                                bean.setRemindTime("");
                                bean.setRemindDays("");
                            }
                            iLocationManager.updateSecurityFence(bean.getId(), et_fence_name.getText().toString(), bean.getRemindWay(), bean.getFenceRange(), bean.getRemindTime(), bean.getRemindDays(), bean.getFenceLat(), bean.getFenceLng(), new BaseObserverListener<BaseResultBean>() {
                                @Override
                                public void onCompleted() {

                                }

                                @Override
                                public void onError(Throwable e) {
                                    ToastUtils.showShort(AddElectronicFenceActivity.this, getString(R.string.data_error));

                                }

                                @Override
                                public void onNext(BaseResultBean baseResultBean) {
                                    if (baseResultBean.getCode() == 0) {
                                        bean.setName(et_fence_name.getText().toString());
                                        ToastUtils.showShort(AddElectronicFenceActivity.this, getString(R.string.upload_success));
                                        ElectronicFenceEvent event = new ElectronicFenceEvent();
                                        event.setBean(bean);
                                        event.setNew(false);
                                        event.setPosition(getIntent().getIntExtra("position", 0));
                                        EventBus.getDefault().post(event);
                                    }
                                }
                            });
                        }
                    }
                }
            });

        } else {
            header_title.setText(getString(R.string.add_fence));
            bean = new ElectronicFenceBean();

            iLocationManager.getLastLocation(new BaseObserverListener<BaseResultBean<LocationBean>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    ToastUtils.showShort(AddElectronicFenceActivity.this, getString(R.string.data_error));
                }

                @Override
                public void onNext(BaseResultBean<LocationBean> baseResultBean) {
                    LatLng latLng = new LatLng(Double.parseDouble(baseResultBean.getData().getLat()),Double.parseDouble(baseResultBean.getData().getLng()));
                    CameraUpdate mCameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng,15,0,0));
                    aMap.moveCamera(mCameraUpdate);

                    marker = addMarkerToMap(latLng, BitmapFactory.decodeResource(getResources(), R.drawable.location_marker), 0.5f, 0.9f, false, 0);
                    circle = addCircleToMap(latLng,RANGE_MIN, getResources().getColor(R.color.blue_4));
                    bean.setRemindWay(1);
                    bean.setFenceLat(baseResultBean.getData().getLat());
                    bean.setFenceLng(baseResultBean.getData().getLng());
                    bean.setFenceRange(RANGE_MIN);
                    bean.setEnable(true);
                }
            });

            header_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iLocationManager != null) {
                        if (et_fence_name.getText().toString().equals("")) {
                            ToastUtils.showShort(AddElectronicFenceActivity.this,getString(R.string.plz_input_fence_name));
                        }else {
                            if (bean.getRemindWay() == 3 && (bean.getRemindTime() == null || bean.getRemindDays() == null || bean.getRemindTime().equals("") || bean.getRemindDays().equals(""))) {
                                mDialog = new CustomDialog(AddElectronicFenceActivity.this, getString(R.string.warm_prompt), getString(R.string.fence_notice), getString(R.string.confirm), new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        mDialog.dismiss();
                                    }
                                });
                                mDialog.setCanotBackPress();
                                mDialog.setCanceledOnTouchOutside(false);
                                mDialog.show();
                            } else {
                                iLocationManager.addSecurityFence(et_fence_name.getText().toString(), bean.getRemindWay(), bean.getFenceRange(), bean.getRemindTime(), bean.getRemindDays(), bean.getFenceLat(), bean.getFenceLng(), new BaseObserverListener<BaseResultBean>() {
                                    @Override
                                    public void onCompleted() {

                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                        ToastUtils.showShort(AddElectronicFenceActivity.this, getString(R.string.data_error));
                                    }

                                    @Override
                                    public void onNext(BaseResultBean baseResultBean) {
                                        if (baseResultBean.getCode() == 0) {
                                            bean.setName(et_fence_name.getText().toString());
                                            ToastUtils.showShort(AddElectronicFenceActivity.this, getString(R.string.upload_success));
                                            ElectronicFenceEvent event = new ElectronicFenceEvent();
                                            event.setBean(bean);
                                            event.setNew(true);
                                            event.setPosition(getIntent().getIntExtra("position", 0));
                                            EventBus.getDefault().post(event);
                                            finish();
                                        }
                                    }
                                });
                            }
                        }
                    }
                }
            });
        }

        aMap.setOnMapClickListener(new AMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                bean.setFenceLat(String.valueOf(latLng.latitude));
                bean.setFenceLng(String.valueOf(latLng.longitude));
                ((Circle) circle).setCenter(latLng);
                aMap.animateCamera(CameraUpdateFactory.changeLatLng(latLng), 500, null);
                ((Marker) marker).setPosition(latLng);
            }
        });

    }

    private void onClick(){
        sb_fence_range.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv_fence_range.setText(String.valueOf(progress+RANGE_MIN)+getString(R.string.meter));
                double step = sb_fence_range.getWidth() / (double)RANGE;
                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rl_fence_range.getLayoutParams();
                lp.setMargins((int)(progress * step),0,0,0);
                rl_fence_range.setLayoutParams(lp);
                ((Circle) circle).setRadius(progress+RANGE_MIN);
                bean.setFenceRange(progress+RANGE_MIN);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        rg_remind_way.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                isExtend = !isExtend;
                iv_remind_way.setImageResource(isExtend ? R.drawable.ic_arrow_right : R.drawable.ic_arrow_down);
                rg_remind_way.setVisibility(isExtend ? View.GONE : View.VISIBLE);

            }
        });
        rb_remind_enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bean.setRemindWay(1);
            }
        });

        rb_remind_leave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bean.setRemindWay(2);
            }
        });
        rb_remind_not_enter_on_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bean.setRemindWay(3);
                //initial PopWindow 防止每次点击修改周期和时间值但是不保存导致数据不正确的问题
                popupWindow = new CommonPopupWindow(AddElectronicFenceActivity.this,R.layout.electronic_fence_timing,(int)(ScreenAdapterUtil.getWidthPx(AddElectronicFenceActivity.this)*0.9),(int)(ScreenAdapterUtil.getHeightPx(AddElectronicFenceActivity.this)*0.7)) {
                    private NumberPickerView hour;
                    private NumberPickerView minute;
                    private TextView tv_confirm;
                    private TextView tv_cancel;
                    private TextView weekday_7;
                    private TextView weekday_6;
                    private TextView weekday_5;
                    private TextView weekday_4;
                    private TextView weekday_3;
                    private TextView weekday_2;
                    private TextView weekday_1;

                    @Override
                    protected void initView() {
                        View view = getContentView();
                        hour = view.findViewById(R.id.hour);
                        minute = view.findViewById(R.id.minute);
                        tv_confirm = view.findViewById(R.id.tv_confirm);
                        tv_cancel = view.findViewById(R.id.tv_cancel);
                        weekday_1 = view.findViewById(R.id.weekday_1);
                        weekday_2 = view.findViewById(R.id.weekday_2);
                        weekday_3 = view.findViewById(R.id.weekday_3);
                        weekday_4 = view.findViewById(R.id.weekday_4);
                        weekday_5 = view.findViewById(R.id.weekday_5);
                        weekday_6 = view.findViewById(R.id.weekday_6);
                        weekday_7 = view.findViewById(R.id.weekday_7);

                        final String[] hourArray = TimeUtils.returnStringArray(0,23);
                        final String[] minuteArray = TimeUtils.returnStringArray(0,59);

                        hour.setDisplayedValues(hourArray);
                        hour.setMinValue(0);
                        hour.setMaxValue(hourArray.length-1);

                        minute.setDisplayedValues(minuteArray);
                        minute.setMinValue(0);
                        minute.setMaxValue(minuteArray.length-1);

                        if (bean != null) {
                            if (bean.getRemindTime() != null && !bean.getRemindTime().equals("")) {
                                hour.setValue(Integer.parseInt(bean.getRemindTime().split(":")[0]));
                                minute.setValue(Integer.parseInt(bean.getRemindTime().split(":")[1]));
                            } else {
                                hour.setValue(TimeUtils.getCurrentHour24());
                                minute.setValue(TimeUtils.getCurrentMinute());
                            }
                            if (bean.getRemindDays() != null) {
                                String[] weeks = bean.getRemindDays().split(",");
                                for (String week : weeks) {
                                    switch (week) {
                                        case "1":
                                            weekArray[0] = !weekArray[0];
                                            weekday_1.setBackgroundResource(R.drawable.electronic_fence_week_select);
                                            weekday_1.setTextColor(getResources().getColor(R.color.white_1));
                                            break;
                                        case "2":
                                            weekArray[1] = !weekArray[1];
                                            weekday_2.setBackgroundResource(R.drawable.electronic_fence_week_select);
                                            weekday_2.setTextColor(getResources().getColor(R.color.white_1));
                                            break;
                                        case "3":
                                            weekArray[2] = !weekArray[2];
                                            weekday_3.setBackgroundResource(R.drawable.electronic_fence_week_select);
                                            weekday_3.setTextColor(getResources().getColor(R.color.white_1));
                                            break;
                                        case "4":
                                            weekArray[3] = !weekArray[3];
                                            weekday_4.setBackgroundResource(R.drawable.electronic_fence_week_select);
                                            weekday_4.setTextColor(getResources().getColor(R.color.white_1));
                                            break;
                                        case "5":
                                            weekArray[4] = !weekArray[4];
                                            weekday_5.setBackgroundResource(R.drawable.electronic_fence_week_select);
                                            weekday_5.setTextColor(getResources().getColor(R.color.white_1));
                                            break;
                                        case "6":
                                            weekArray[5] = !weekArray[5];
                                            weekday_6.setBackgroundResource(R.drawable.electronic_fence_week_select);
                                            weekday_6.setTextColor(getResources().getColor(R.color.white_1));
                                            break;
                                        case "7":
                                            weekArray[6] = !weekArray[6];
                                            weekday_7.setBackgroundResource(R.drawable.electronic_fence_week_select);
                                            weekday_7.setTextColor(getResources().getColor(R.color.white_1));
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                        }else {
                            hour.setValue(TimeUtils.getCurrentHour24());
                            minute.setValue(TimeUtils.getCurrentMinute());
                        }
                    }

                    @Override
                    protected void initEvent() {
                        weekday_1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                weekday_1.setBackgroundResource(weekArray[0] ? R.drawable.electronic_fence_week_normal : R.drawable.electronic_fence_week_select);
                                weekday_1.setTextColor(getResources().getColor(weekArray[0] ? R.color.gray_7 : R.color.white_1));
                                weekArray[0] = !weekArray[0];
                            }
                        });

                        weekday_2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                weekday_2.setBackgroundResource(weekArray[1] ? R.drawable.electronic_fence_week_normal : R.drawable.electronic_fence_week_select);
                                weekday_2.setTextColor(getResources().getColor(weekArray[1] ? R.color.gray_7 : R.color.white_1));
                                weekArray[1] = !weekArray[1];
                            }
                        });

                        weekday_3.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                weekday_3.setBackgroundResource(weekArray[2] ? R.drawable.electronic_fence_week_normal : R.drawable.electronic_fence_week_select);
                                weekday_3.setTextColor(getResources().getColor(weekArray[2] ? R.color.gray_7 : R.color.white_1));
                                weekArray[2] = !weekArray[2];
                            }
                        });

                        weekday_4.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                weekday_4.setBackgroundResource(weekArray[3] ? R.drawable.electronic_fence_week_normal : R.drawable.electronic_fence_week_select);
                                weekday_4.setTextColor(getResources().getColor(weekArray[3] ? R.color.gray_7 : R.color.white_1));
                                weekArray[3] = !weekArray[3];
                            }
                        });

                        weekday_5.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                weekday_5.setBackgroundResource(weekArray[4] ? R.drawable.electronic_fence_week_normal : R.drawable.electronic_fence_week_select);
                                weekday_5.setTextColor(getResources().getColor(weekArray[4] ? R.color.gray_7 : R.color.white_1));
                                weekArray[4] = !weekArray[4];
                            }
                        });

                        weekday_6.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                weekday_6.setBackgroundResource(weekArray[5] ? R.drawable.electronic_fence_week_normal : R.drawable.electronic_fence_week_select);
                                weekday_6.setTextColor(getResources().getColor(weekArray[5] ? R.color.gray_7 : R.color.white_1));
                                weekArray[5] = !weekArray[5];
                            }
                        });

                        weekday_7.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                weekday_7.setBackgroundResource(weekArray[6] ? R.drawable.electronic_fence_week_normal : R.drawable.electronic_fence_week_select);
                                weekday_7.setTextColor(getResources().getColor(weekArray[6] ? R.color.gray_7 : R.color.white_1));
                                weekArray[6] = !weekArray[6];
                            }
                        });

                        tv_cancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                popupWindow.getPopupWindow().dismiss();
                            }
                        });

                        tv_confirm.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                bean.setRemindTime(hour.getContentByCurrValue()+":"+minute.getContentByCurrValue());
                                if (arrayToString(weekArray) == null) {
                                    mDialog = new CustomDialog(AddElectronicFenceActivity.this, getString(R.string.warm_prompt), getString(R.string.fence_notice),getString(R.string.confirm),new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            mDialog.dismiss();
                                        }
                                    });
                                    mDialog.setCanotBackPress();
                                    mDialog.setCanceledOnTouchOutside(false);
                                    mDialog.show();
                                }else {
                                    bean.setRemindDays(arrayToString(weekArray));
                                    popupWindow.getPopupWindow().dismiss();
                                }
                            }
                        });
                    }
                };
                layoutGravity = new CommonPopupWindow.LayoutGravity(CommonPopupWindow.LayoutGravity.CENTER_HORIZONTAL | CommonPopupWindow.LayoutGravity.CENTER_VERTICAL);
                popupWindow.showBashOfAnchor(ll_parent, layoutGravity, 0, 0);
            }
        });

        rl_remind_way.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isExtend = !isExtend;
                iv_remind_way.setImageResource(isExtend ? R.drawable.ic_arrow_right : R.drawable.ic_arrow_down);
                rg_remind_way.setVisibility(isExtend ? View.GONE : View.VISIBLE);
            }
        });

        header_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    public Object addMarkerToMap(LatLng latLng, Bitmap bitmap, float anchorX, float anchorY, boolean isNeedAnimation, long displayTime) {
        Object marker = null;
        if (latLng != null && aMap != null) {
            aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
            BitmapDescriptor bitmapDesAmap = BitmapDescriptorFactory.fromBitmap(bitmap);
            marker = aMap.addMarker(new MarkerOptions().position(latLng).icon(bitmapDesAmap).anchor(anchorX, anchorY).draggable(false));
            if (isNeedAnimation) {
                jump(latLng.latitude, latLng.longitude, marker);
            }
            if (displayTime > 0) {
                removeMarkerDelay(marker, displayTime);
            }
        }
        return marker;
    }

    private void removeMarkerDelay(final Object marker, long displayTime) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (marker instanceof Marker) {
                    ((Marker) marker).remove();
                }
            }
        }, displayTime);
    }

    private void jump(final double lat, final double lng, final Object marker) {
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        final long duration = 1500;
        final Interpolator interpolator = new BounceInterpolator();

        handler.post(new Runnable() {
            @Override
            public void run() {
                long elapsed = SystemClock.uptimeMillis() - start;
                float t = interpolator.getInterpolation((float) elapsed / duration);
                if (aMap == null) {
                    return;
                }
                LatLng latLngAmap = new LatLng(lat, lng);
                Projection projAmap = aMap.getProjection();
                if (projAmap != null) {
                    Point startPointAmap = projAmap.toScreenLocation(latLngAmap);
                    startPointAmap.offset(0, -100);
                    LatLng startLatLngAmap = projAmap.fromScreenLocation(startPointAmap);
                    double latAmap = t * latLngAmap.latitude + (1 - t)
                            * startLatLngAmap.latitude;
                    double lngAmap = t * latLngAmap.longitude + (1 - t)
                            * startLatLngAmap.longitude;
                    if (marker instanceof Marker) {
                        ((Marker) marker).setPosition(new LatLng(latAmap, lngAmap));
                    }
                }
                if (t < 1.0) {
                    handler.postDelayed(this, 16);
                }
            }
        });
    }

    private Object addCircleToMap(LatLng latLng, double radius, int color) {
        Object circle = null;
        if (latLng != null && aMap != null) {
            circle = aMap.addCircle(new CircleOptions().center(latLng).radius(radius).fillColor(color).strokeWidth(0));
        }
        return circle;
    }

    private String arrayToString(boolean[] arrays) {
        StringBuilder sb = new StringBuilder("");
        for (int i=1;i<=arrays.length;i++) {
            if (arrays[i-1]) {
                sb.append(i+",");
            }
        }
        if (sb.toString().equals("")){
            return null;
        } else {
            StringBuilder stringBuilder = new StringBuilder(sb.substring(0, sb.length() - 1));
            return stringBuilder.toString();
        }
    }
}
